import type { LoginResult } from '@/types/member';
import { defineStore } from 'pinia';
import { ref } from 'vue';

// 定义store
export const userStore = defineStore(
  'user',
  () => {
    const profile = ref<LoginResult>();

    // 保存用户信息，登录时使用
    const setInfo = (val: LoginResult) => {
      profile.value = val;
    };

    // 清除用户信息，退出时使用
    const clearInfo = () => {
      profile.value = undefined;
    };

    return {
      profile,
      setInfo,
      clearInfo,
    };
  },
  {
    persist: {
      storage: {
        getItem(key: any) {
          return uni.getStorageSync(key);
        },
        setItem(key: string, value: any) {
          uni.setStorageSync(key, value);
        },
      },
    },
  }
);
